package com.example._03vehicle.controller;

import com.example._03vehicle.base.response.JsonResult;
import com.example._03vehicle.base.response.StatusCode;
import com.example._03vehicle.mapper.VehicleMapper;
import com.example._03vehicle.pojo.dto.VehicleAddParam;
import com.example._03vehicle.pojo.dto.VehicleListQuery;
import com.example._03vehicle.pojo.entity.Vehicle;
import com.example._03vehicle.pojo.vo.UserVO;
import com.example._03vehicle.pojo.vo.VehicleListVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.sun.org.apache.xerces.internal.util.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.List;
@Validated
@Slf4j
@Api(tags = "02.车辆管理模块")
@RequestMapping(value = "/v1/vehicle")
@RestController
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping(value = "/add")
    @ApiOperation(value = "添加车辆功能")
    @ApiOperationSupport(order = 10)
    @ApiImplicitParam(name = "vehicleAddParam", value = "车辆添加参数", required = true, dataType = "VehicleAddParam")
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, @ApiIgnore HttpSession session) {
        log.debug("vehicleaddparam:" + vehicleAddParam);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        UserVO uservo = (UserVO) session.getAttribute("user");
//        if (uservo == null) {
//            return JsonResult.notlogin_error();
//        } else {
            vehicle.setUserId(uservo.getId());
            vehicle.setCreateTime(new Date());
            vehicle.setUpdateTime(new Date());
            int num = vehicleMapper.insertByVehicle(vehicle);
            if (num > 0) {
                return JsonResult.success(num);
            } else {
                return JsonResult.fail();
            }
        //}
    }

    @GetMapping(value = "/list")
    @ApiOperation(value = "查询车辆列表功能")
    @ApiOperationSupport(order = 20)
    @ApiImplicitParam(name = "vehicleListQuery", value = "车辆查询参数", required = true, dataType = "VehicleListQuery")
    public JsonResult listVehicle( VehicleListQuery vehicleListQuery, @ApiIgnore HttpSession session) {
        log.debug("vehiclelistquery" + vehicleListQuery);
        UserVO uservo = (UserVO) session.getAttribute("user");
        if (uservo == null) {
            return JsonResult.notlogin_error();
        } else {
            Long userId=uservo.getId();
            vehicleListQuery.setUserId(userId);
            List<VehicleListVo> list = vehicleMapper.listVehicle(vehicleListQuery);
            return JsonResult.success(list);
        }


    }
    //get方法路径参数

    @GetMapping(value = "/delete/{id}")
    @ApiOperation(value = "删除车辆功能")
    @ApiOperationSupport(order = 30)
    public JsonResult deleteVehicle(@Range(min=1,max=23,message = "车辆编号必须得在1-100之间")@PathVariable  Long id, @ApiIgnore HttpSession session) throws Exception{
        log.debug("id:" + id);
        //这是手动抛出，标识为非法参数
//        if(id<0){
//             //return new JsonResult(StatusCode.OPERATION_FAIL,"id非法");
//            throw new RuntimeException("id值非法");
//
//        }
        UserVO uservo = (UserVO) session.getAttribute("user");

        if (uservo == null) {
            return JsonResult.notlogin_error();
        } else {
            Long userId = uservo.getId();
            Integer num = vehicleMapper.deleteVehicle(id,userId );
            log.debug(num+"num");
            if (num >0) {
                return JsonResult.success(num);
            } else {
                return JsonResult.crimeNotice();
            }
        }
    }
}
